package com.example.app;

import android.app.Activity;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;

import cc.vv.lklibrary.log.LogOperate;

/**
 * activity栈管理
 * @author chaohaipeng  on 2018/8/28.
 */

public class ActivityStackManager {
    public List<WeakReference<Activity>> mActivityList = new LinkedList<>();

    private static ActivityStackManager mInstance;

    public static ActivityStackManager getInstance() {
        if (null == mInstance) {
            synchronized (ActivityStackManager.class) {
                if (null == mInstance) {
                    mInstance = new ActivityStackManager();
                }
            }
        }
        return mInstance;
    }


    /**
     * 添加activity到容器中
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (activity != null) {
            mActivityList.add(new WeakReference<>(activity));
        }
    }

    /**
     * 遍历所有的Activity并finish
     */
    public void exitApp() {
        try {
            for (int i = 0; i < mActivityList.size(); i++) {
                WeakReference<Activity> activityWeakReference = mActivityList.get(i);
                if (activityWeakReference == null) {
                    continue;
                }
                Activity activity = activityWeakReference.get();
                if (null != activity) {
                    activity.finish();
                }
            }
            mActivityList.clear();
        } catch (Exception e) {
            LogOperate.e(e.getMessage(), e);
        }
    }

    /**
     * 遍历指定的Activity并finish
     */
    public void exit(int num) {
        for (int i = 1; i <= num; i++) {
            if (mActivityList.size() >= num) {
                WeakReference<Activity> activityWeakReference = mActivityList.get(mActivityList.size() - i);
                Activity activity = activityWeakReference.get();
                if (activity != null) {
                    activity.finish();
                }
            }
        }
    }


    /**
     * 从集合中移除最近的一个activity并finish
     */
    public void finish() {
        WeakReference<Activity> activityWeakReference = mActivityList.get(mActivityList.size() - 1);
        if (activityWeakReference == null) {
            return;
        }
        Activity activity = activityWeakReference.get();
        if (activity != null) {
            activity.finish();
        }
        mActivityList.remove(mActivityList.size() - 1);
    }

    /**
     * 返回当前显示的activity
     *
     * @return
     */
    public Activity getCurrentActivity() {
        Activity activity = null;
        if (mActivityList != null && mActivityList.size() >= 1) {
            WeakReference<Activity> activityWeakReference = mActivityList.get(mActivityList.size() - 1);
            if (activityWeakReference != null) {
                activity = activityWeakReference.get();
            }
        }
        return activity;
    }

    /**
     * 从集合中移除activity
     */
    public void remove(Activity activityP) {
        if (activityP != null && mActivityList != null && mActivityList.size() >= 1) {
            try {
                for (int i = mActivityList.size() - 1; i >= 0; i--) {
                    WeakReference<Activity> activityWeakReference = mActivityList.get(i);
                    if (activityWeakReference == null) {
                        continue;
                    }
                    Activity activity = activityWeakReference.get();
                    if (activityP == activity) {
                        mActivityList.remove(i);
                        break;
                    }
                }
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
            }
        }
    }
}
